package com.yakuj3.android.comptadroid.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.widget.RemoteViews;

import com.yakuj3.android.comptadroid.MainActivity;
import com.yakuj3.android.comptadroid.R;
import com.yakuj3.android.comptadroid.constantes.Constantes;

public class ComptaDroidWidget extends AppWidgetProvider {

	private String month = "";
	private double montant;
	private double epargne;
	private SharedPreferences _sp;
	
	@Override
	  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
	    super.onUpdate(context, appWidgetManager, appWidgetIds);
	    
	    _sp = PreferenceManager.getDefaultSharedPreferences(context);
	    
	    final int length = appWidgetIds.length;
	    
	    if (month.equalsIgnoreCase(""))
	    	populateInformationFromSP();
	    
	    for (int i = 0 ; i < length ; i++) {
	    	
	    	RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.app_widget_layout);
	    	
	    	Intent comptaDroidActivity = new Intent(context, MainActivity.class);
	    	PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, comptaDroidActivity, 0);
	    	views.setOnClickPendingIntent(R.id.widget_icon, configPendingIntent); 
	    	
    		views.setTextViewText(R.id.widget_month, month);
    		views.setTextViewText(R.id.widget_montant, "+ " + montant);
    		if (this.montant < Constantes.LOWER_BORNE)
	    		views.setTextColor(R.id.widget_montant, context.getResources().getColor(R.color.red));
	    	else if (this.montant > Constantes.HIGHER_BORNE)
	    		views.setTextColor(R.id.widget_montant, context.getResources().getColor(R.color.green));
	    	else
	    		views.setTextColor(R.id.widget_montant, context.getResources().getColor(R.color.orange));
    		views.setTextViewText(R.id.widget_epargne_montant, "+ " + epargne);
    		if (this.epargne < Constantes.LOWER_E_BORNE)
    			views.setTextColor(R.id.widget_epargne_montant, context.getResources().getColor(R.color.red));
    		else if (this.epargne > Constantes.HIGHER_E_BORNE)
	    		views.setTextColor(R.id.widget_epargne_montant, context.getResources().getColor(R.color.green));
	    	else
	    		views.setTextColor(R.id.widget_epargne_montant, context.getResources().getColor(R.color.orange));
	    	views.setTextViewText(R.id.widget_epargne, "Epargne");
	    	
	    	appWidgetManager.updateAppWidget(appWidgetIds[i], views);
	    }
	        
	    saveInformationToSP();
	}
	
	@Override
	  public void onReceive(Context context, Intent intent) {
		
		String action = intent.getAction(); 
		if (action != null && action.equals(Constantes.UPDATE_WIDGET))
		{
			final AppWidgetManager manager = AppWidgetManager.getInstance (context);
			
			this.epargne = Math.round(intent.getFloatExtra(Constantes.WIDGET_EXTRA_EPARGNE, -99999) * 100.0) / 100.0;
			this.month = intent.getStringExtra(Constantes.WIDGET_EXTRA_MONTH);
			this.montant = Math.round(intent.getFloatExtra(Constantes.WIDGET_EXTRA_MONTANT, -99999) * 100.0) / 100.0;
			
			onUpdate(context, manager, manager.getAppWidgetIds(new ComponentName(context, ComptaDroidWidget.class)));
		}
		else
			super.onReceive(context, intent);
	}
	
	private void populateInformationFromSP()
	{
		month = _sp.getString(Constantes.SP_WD_MONTH, "ERROR_SP");
		String temp = _sp.getString(Constantes.SP_WD_MONTANT, "ERROR_SP");
		if (!temp.equalsIgnoreCase("error_sp"))
		{
			montant = Math.round(Float.parseFloat(temp) * 100.0) / 100.0;
		}
		temp = _sp.getString(Constantes.SP_CURRENT_EPARGNE, "ERROR_SP");
		if (!temp.equalsIgnoreCase("error_sp"))
		{
			epargne = Math.round(Float.parseFloat(temp) * 100.0) / 100.0;
		}
	}
	
	private void saveInformationToSP()
	{
		Editor edit = _sp.edit();
		edit.putString(Constantes.SP_WD_MONTH, month);
		edit.putString(Constantes.SP_WD_MONTANT, "" + montant);
		edit.putString(Constantes.WIDGET_EXTRA_EPARGNE, "" + epargne);
		edit.commit();
	}
}
